package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    Page<TrackListVo> selectTrackPage(Page<TrackListVo> page, @Param("vo") TrackInfoQuery trackInfoQuery);


    //@Update("update track_info set order_num =order_num-1 where album_id=#{albumId} and is_deleted=0 and order_num>#{orderNum}")
    @Update("update track_info set order_num = order_num - 1 where order_num > #{orderNum} and album_id = #{albumId} and is_deleted = 0")
    void updateTrackNum(@Param("orderNum") Integer orderNum,@Param("id") Long id);


    //这里要与前端需要的返回类型中的trackId保持一致，不然前端接收不到
    @Select("select info.id trackId,\n" +
            "       info.track_title,\n" +
            "       info.cover_url,\n" +
            "       info.media_duration,\n" +
            "       info.order_num,\n" +
            "       info.create_time,\n" +
            "       max(if(stat.id = '0701', stat.stat_num, 0)) playStatNum,\n" +
            "       max(if(stat.id = '0704', stat.stat_num, 0)) commentStat\n" +
            "from track_info info\n" +
            "         inner join track_stat stat on info.id = stat.track_id\n" +
            "where info.album_id = #{albumId}  and info.is_deleted = 0\n" +
            "group by info.id")
    IPage<AlbumTrackListVo> selectAlbumTrackList(Page<AlbumTrackListVo> albumTrackListVoPage,@Param("albumId") Long albumId);


    @Update("update track_stat set stat_num=stat_num+#{count} " +
            "where stat_type = #{statType} " +
            "and track_id=#{trackId} " +
            "and is_deleted = 0;")
    void updateTrackStat(@Param("trackId") Long trackId,@Param("count") Integer count,@Param("statType") String statType);
    //,@Param("userId") Long userId
}
